package
{
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.utils.Timer;

	public class MP_Man
	{
		private static var _ins:MP_Man;
		public static function get ins():MP_Man
		{
			if(!_ins){
				_ins = new MP_Man();
			}
			return _ins;
		}
		
		public var mTimer:Timer;
		private var urlArr:Array = [];
		private var curTime:int;
		
		private var copyArr:Array = [];
		
		public function MP_Man()
		{
			mTimer = new Timer(16);
			mTimer.addEventListener(TimerEvent.TIMER, onTimerEvent);
			mTimer.start();
		}
		public function pushPath(path:String):void
		{
			var index:int = path.indexOf("bg_tiles");
			if(index == -1){
				index = path.indexOf("tiles");
			}
			if(index != -1){
				if(path.indexOf(".mp") != -1){
					urlArr.push(path);
				}
			}
			else{
				copyArr.push(path);
			}
		}
		private function onTimerEvent(e:TimerEvent):void
		{
			var time:int = new Date().time;
			if(time - curTime < 60){
				if(urlArr.length){
					MP_Decode.printMsg.text = "正在转换，还剩余：" + urlArr.length;
					var curUrl:String = urlArr.shift();
					try{
						new MP_Decode().doLoad(curUrl);
					}
					catch(e:*){
						trace("转换出错，路径为：" + curUrl);
					}
				}
				else{
					MP_Decode.printMsg.text = "转换完成！";
					
					mTimer.removeEventListener(TimerEvent.TIMER, onTimerEvent);
					mTimer.start();
					
					mTimer.addEventListener(TimerEvent.TIMER, onTimerEvent2);
				}
			}
			curTime = time;
		}
		private function onTimerEvent2(e:TimerEvent):void
		{
			return;
			var time:int = new Date().time;
			if(time - curTime < 60){
				if(copyArr.length){
					MP_Decode.printMsg.text = "转换完成，正在复制相关文件，还剩余：" + copyArr.length;
					
					var curPath:String = copyArr.pop();
					
					var path:String = curPath.replace(MP_Decode.importRoot, "");
					if(path.indexOf("\\") == 0){
						path = path.substring(1);
					}
					
					var rootFile:File = new File(MP_Decode.importRoot);
					var fl1:File = rootFile.resolvePath(path);
					
					rootFile = new File(MP_Decode.exportUrl);
					var fl2:File = rootFile.resolvePath(path);
					
					fl1.copyTo(fl2, true);
				}
				else{
					MP_Decode.printMsg.text = "恭喜，全部搞定！";
					
				}
			}
			curTime = time;
		}
	}
}